var express = require('express');
var router = express.Router();
var mongo = require('mongodb');
var mongoose = require('mongoose');
var ObjectId = require('mongoose').Types.ObjectId;
var Keyword = require('../models/keywords').Keyword;
var KeywordStat = require('../models/keywordStats').KeywordStat;
var Wechat = require('../models/wechat').Wechat;
var WechatStat = require('../models/wechatStat').WechatStat;


function DoubleBubbleSort(arr1, arr2) {
    for (var i = 0; i < arr1.length; i++) {
        for (var j = i; j < arr1.length; j++) {
            if (arr1[i] < arr1[j]) {
                var temp = arr1[i];
                arr1[i] = arr1[j];
                arr1[j] = temp;

                //second
                var temp2 = arr2[i];
                arr2[i] = arr2[j];
                arr2[j] = temp2;

            }
        }
    }
    return {
        'arr1': arr1,
        'arr2': arr2
    };

}

// 列表页

router.get('/index', function (req, res) {
    var docs = {
        'title': '微信',
        'function': [
            {
                'name': '阅读量/点赞量',
                'link': '/wechat_stat_playlike'
            },
            {
                'name': '倾向性',
                'link': '/wechat_stat_tendency'
            },
            {
                'name': '关键词',
                'link': '/wechat_stat_relate_words'
            }
        ]
    };
    res.render('pages/wechat_stat/index', {
        layout: 'layout/admin',
        controller: 'wechat_stat',
        action: 'index',
        data: {
            docs: docs,
            title: 'hello'
        }
    });
});

router.get('/wechat_stat', function (req, res) {
    var query = req.query.q;
    var re = new RegExp(String(query));
    var ret = {};
    Wechat.find({$or: [
        {'title': re},
        {'trim_content.content': re}
    ]}, {
        post_time: 1,
        author: 1,
        like_num: 1,
        read_num: 1,
        title: 1,
        trim_content: 1,
        keywords: 1,
        sentiment: 1
    }).sort({
        'post_time': 1
    }).lean().exec(function (err, docs) {
        if (err) {
            console.log('error');
        }
        ret.status = true;
        ret.data = docs;

        res.json(ret);

    });
});

router.get('/keyword', function (req, res) {
    var ret = {};
    Keyword.find({}).lean().exec(function (err, docs) {
        if (err) {
            console.log('error');
        }

        ret.status = true;
        ret.data = docs;
        res.json(ret);
    });
});

//keywords title集合
router.get('/array', function (req, res) {
    var list = [];
    Keyword.find().lean().exec(function (err, docs) {
        if (err) {
            console.log('error');
        }

        //init array
        for (var i in docs) {
            var arr = [];
            arr.push(docs[i].title);
            for (var j in docs[i].nicks) {
                if (docs[i].nicks != '') {
                    arr.push(docs[i].nicks[j]);
                }
            }
            list.push(arr);
        }


        res.render('pages/wechat_stat/index', {
            layout: 'layout/admin',
            controller: 'wechat_stat',
            action: 'index',
            data: {
                docs: list
            }
        });

    });
});

// 阅读量/点赞量
router.get('/wechat_stat_playlike', function (req, res) {
    //url: http://localhost:18222/wechat_stat_playlike/
    var list = [], ret = {}, date = [], allDate = [], tempData = {};
    var insertData = {};
    Keyword.find({}).lean().exec(function (err, docs) {
        if (err) {
            console.log('error');
        }

        for (var i in docs) {
            list.push({
                'keyword_id': docs[i]._id,
                'title': docs[i].title
            })
        }

        Date.prototype.Format = function (fmt) { //author: meizz
            var o = {
                "M+": this.getMonth() + 1, //月份
                "d+": this.getDate(), //日
                "h+": this.getHours(), //小时
                "m+": this.getMinutes(), //分
                "s+": this.getSeconds(), //秒
                "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                "S": this.getMilliseconds() //毫秒
            };
            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
                if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        };

        list.forEach(function (obj, index) {
            if (obj.title != null) {
                console.log('dddd:' + obj.title + ' id:' + obj.keyword_id);
                var keyword_id = obj.keyword_id, title = obj.title;
                Wechat.findByList(obj.title, function (err, data) {
                    for (var d in data) {
                        var unixTimestamp = data[d].post_time;
                        var commonTime = new Date(unixTimestamp).Format("yyyy/MM/dd");
                        if (!hashMap.Contains(commonTime)) {
                            //not exist
                            hashMap.Set(commonTime, commonTime);
                            tempData[commonTime] = {
                                'date': commonTime,
                                'read_num': parseInt(data[d].read_num),
                                'like_num': parseInt(data[d].like_num)
                            }
                        }
                        else {
                            //exist
                            tempData[commonTime].read_num += Number(parseInt(data[d].read_num));
                            tempData[commonTime].like_num += Number(parseInt(data[d].like_num));
                        }
                    }
                    console.log(title);
                    console.log(tempData);

                    var exportData = [];
                    for (var t in tempData) {
                        exportData.push(tempData[t])
                    }

                    console.log(title);
                    console.log(exportData);

                    insertData = new WechatStat({
                        'stat_time': Number(String(Date.parse(new Date())).substring(0, 10)),
                        'keyword_id': new ObjectId(keyword_id),
                        'title': title,
                        'wechat_playlike': exportData
                    });

                    insertData.save(insertData, function (err) {
                        if (err) {
                            console.log(err);
                        }
                        console.log('insert success one.!!!!!!!!!!!!!!!!!!!!!');
                    });
                    //             console.log(insertData);

                });

            }

        });


    });


    //init
    var hashMap = {
        Set: function (key, value) {
            this[key] = value
        },
        Get: function (key) {
            return this[key]
        },
        Contains: function (key) {
            return this.Get(key) == null ? false : true
        },
        Remove: function (key) {
            delete this[key]
        }
    };
});


//关键词
router.get('/wechat_stat_relate_words', function (req, res) {
    //url: http://localhost:18222/wechat_stat_relate_words/
    var list = [], ret = {}, date = [], allDate = [];
    //微信公众号类型映射
    var wechat_type = {
        'dianshitai': ["湖南卫视", "安徽卫视", "江苏卫视", "浙江卫视中国蓝", "广东卫视", "山东卫视", "湖北卫视", "北京卫视", "东方卫视", "云南卫视", "天津卫视", "CCTV电视剧", "央视综艺", "央视新闻", "央视财经", "深圳卫视"],
        'shipinwangzhan': ["搜狐视频", "优酷", "土豆", "乐视网", "腾讯视频", "爱奇艺", "优酷娱乐", "腾讯娱乐"],
        'jiemu': ["中国好声音", "中国好歌曲", "我看你有戏", "快乐大本营", "天天向上", "非诚勿扰", "职来职往", "梦想星搭档", "我是歌手", "CCTV1开讲啦", "星光大道", "我们都爱笑", "你会怎么做", "浙江卫视我不是明星", "最强大脑", "爱奇艺奇葩说", "优酷侣行", "鲁豫有约", "超级访问", "非常完美官微", "奔跑吧兄弟"],
        'menhuwangzhan': ["搜狐娱乐", "搜狐新闻客户端", "腾讯娱乐", "网易新闻客户端", "新浪娱乐"],
        'zhuanyemeiti': ["媒介杂志", "传媒内参", "广电独家", "广告门", "媒介360", "综艺", "引力传媒", "广告导报", "成功营销", "麦迪逊邦", "艺恩咨询", "艺恩数据", "梅花网-今日"],
        'zhimei': ["壹读", "中国新闻周刊", "三联生活周刊", "南都娱乐周刊", "新周刊", "看电影杂志", "时尚先生"],
        'pinpai': ["奥迪", "奔驰SMART", "宝马中国", "梅赛德斯奔驰", "伊利爱儿俱乐部", "参观伊利工厂", "伊利", "蒙牛纯甄", "加多宝凉茶", "王老吉", "百雀羚", "欧莱雅男士", "美宝莲", "相宜本草", "宝洁生活家", "大宝天天见", "霸王洗发水", "飘柔", "沙宣", "韩束微营销", "杜蕾斯"],
        'zimeiti': ["东乔西看", "topmarketing", "剧制文化"]
    };
    //过滤词表
    var filterKeywords = ["是", "有", "人", "让", "会", "到", "要", "说", "能", "看", "卫视", "没有", "来", "做", "爱", "电视剧", "网络", "去", "大家", "现场", "行业", "产品", "消费者", "电视", "可", "成为", "网友", "阅读", "剧", "懂", "讲", "称"];


    //hash init
    var hashMap = {
        Set: function (key, value) {
            this[key] = value
        },
        Get: function (key) {
            return this[key]
        },
        Contains: function (key) {
            return this.Get(key) == null ? false : true
        },
        Remove: function (key) {
            delete this[key]
        }
    };

    //微信公众号类型映射 hash赋值
    for (var i in wechat_type) {
        for (var j in wechat_type[i]) {
            hashMap.Set(wechat_type[i][j], i);
        }
    }

    //过滤词表 hash赋值
    for (var i in filterKeywords) {
        hashMap.Set(filterKeywords[i], filterKeywords[i]);
    }

    var insertData = {};
    Keyword.find({}).lean().exec(function (err, docs) {
        if (err) {
            console.log('error');
        }

        //获取有title的文章，list存放keyword_id和title
        for (var i in docs) {
            list.push({
                'keyword_id': docs[i]._id,
                'title': docs[i].title
            })
        }

        //遍历list
        list.forEach(function (obj, index) {
            var tempData = {
                'dianshitai': [],
                'shipinwangzhan': [],
                'jiemu': [],
                'menhuwangzhan': [],
                'zhuanyemeiti': [],
                'zhimei': [],
                'pinpai': [],
                'zimeiti': []
            };

            //分类型，过滤，组词
            // init tempData格式
            //tempData={
            //    'dianshitai': ['a-b','c-d'..],
            //   ..
            //}

            if (obj.title != null) {
                var keyword_id = obj.keyword_id, title = obj.title;
                Wechat.findByList(obj.title, function (err, data) {
                    for (var d in data) {
                        var author = data[d].author;
                        var keywords = data[d].keywords;
                        if (hashMap.Contains(author)) {
                            //exist
                            //init keywords
                            var len = keywords.length, a, b, str, comWord1, comWord2;
                            if (keywords != '') {
                                for (a = 0; a < len; a++) {
                                    //filter
                                    if (!hashMap.Contains(keywords[a])) {
                                        for (b = a + 1; b < len; b++) {
                                            if (!hashMap.Contains(keywords[b])) {
                                                comWord1 = keywords[a] + '-' + keywords[b];
//                                               //comWord2= keywords[b] + '-' + keywords[a];
                                                tempData[hashMap.Get(author)].push(comWord1);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    //去重，统计次数
                    //init Temp
                    //Temp = {
                    //   'dianshitai': {
                    //       {'a-b':10},
                    //       {'c-d':20},..
                    //   },
                    //};
                    var Temp = {
                        'dianshitai': {},
                        'shipinwangzhan': {},
                        'jiemu': {},
                        'menhuwangzhan': {},
                        'zhuanyemeiti': {},
                        'zhimei': {},
                        'pinpai': {},
                        'zimeiti': {}
                    };
                    var p, q, len;
                    for (var t in tempData) {
                        for (p = 0, len = tempData[t].length; p < len; p++) {
                            if (!hashMap.Contains(tempData[t][p]) && tempData[t][p] != '') {
                                //not exist
                                hashMap.Set(tempData[t][p], tempData[t][p]);
                                Temp[t][tempData[t][p]] = 1
                            }
                            else {
                                //exit
                                Temp[t][tempData[t][p]]++;
                            }
                        }
//                        //clean hashmap
                        for (var c in tempData[t]) {
                            hashMap.Remove(tempData[t][c]);
                        }
                    }


                    //排序，获取前五对pair
                    //init exportData
                    var exportData = {
                        'dianshitai': [],
                        'shipinwangzhan': [],
                        'jiemu': [],
                        'menhuwangzhan': [],
                        'zhuanyemeiti': [],
                        'zhimei': [],
                        'pinpai': [],
                        'zimeiti': []
                    };
                    var index, k1, k2, weight;
                    for (var m in Temp) {
                        var arrw = [], arrk = [], bubble = {};
                        for (var n in Temp[m]) {
                            // console.log(n+':'+Temp[m][n]);
                            if (Temp[m][n]) {
                                arrk.push(n);
                                arrw.push(Temp[m][n]);
                            }
                            bubble = DoubleBubbleSort(arrw, arrk);
                        }
                        if (bubble.arr2) {
                            for (var bb = 0; bb < 5; bb++) {
                                console.log(bb + '-------' + bubble.arr2[bb] + ' :  ' + bubble.arr1[bb]);
                                if (bubble.arr2[bb]) {
                                    index = bubble.arr2[bb].indexOf('-');
                                    k1 = bubble.arr2[bb].substring(0, index);
                                    k2 = bubble.arr2[bb].substring(index + 1);
                                    weight = bubble.arr1[bb];
                                    exportData[m].push({
                                        'keyword1': k1,
                                        'keyword2': k2,
                                        'weight': weight
                                    })
                                }
                            }
                        }
                    }

                    var exportData2 = [];
                    exportData2.push(exportData);

                    console.log(exportData2);

                    //intit insertData
                    //插入数据库
                    insertData = new WechatStat({
                        'stat_time': Number(String(Date.parse(new Date())).substring(0, 10)),
                        'keyword_id': new ObjectId(keyword_id),
                        'title': title,
                        'wechat_relate_words': exportData2
                    });
                    insertData.save(insertData, function (err) {
                        if (err) {
                            console.log(err);
                        }
                        console.log('insert success one.!!!!!!!!!!!!!!!!!!!!!');
                    });
                    console.log(insertData);

                });

            }

        });
    });


});


// 倾向性
router.get('/wechat_stat_tendency', function (req, res) {
    //url: http://localhost:18222/wechat_stat_tendency/
    var list = [], ret = {}, sent = 0, tempData = {};
    var exportData = {}, insertData = {};
    Keyword.find({}).lean().exec(function (err, docs) {
        if (err) {
            console.log('error');
        }

        for (var i in docs) {
            list.push({
                'keyword_id': docs[i]._id,
                'title': docs[i].title
            })
        }

        list.forEach(function (obj, index) {
            if (obj.title != null) {
                console.log('dddd:' + obj.title + ' id:' + obj.keyword_id);
                var keyword_id = obj.keyword_id, title = obj.title;

                Wechat.findByList(obj.title, function (err, data) {
                    var sentiment = {
                        "positive": 0,
                        "negative": 0,
                        "neutral": 0
                    };
                    for (var d in data) {
                        sent = data[d].sentiment;
                        console.log(sent);
                        //varify
                        if (sent > 0.49) {
                            //positive
                            sentiment.positive++;
                        }
                        else if (sent > 0 && sent < 0.45) {
                            sentiment.negative++;
                        }
                        else if (sent >= 0.45 && sent <= 0.49) {
                            sentiment.neutral++;
                        }
                    }

                    exportData = [];
                    exportData.push(sentiment);
                    console.log(exportData);

                    insertData = new WechatStat({
                        'stat_time': Number(String(Date.parse(new Date())).substring(0, 10)),
                        'keyword_id': new ObjectId(keyword_id),
                        'title': title,
                        'wechat_tendency': exportData
                    });

                    insertData.save(insertData, function (err) {
                        if (err) {
                            console.log(err);
                        }
                        console.log('insert success one.!!!!!!!!!!!!!!!!!!!!!');
                    });
                    console.log(insertData);

                });

            }

        });


    });


    //init
    var hashMap = {
        Set: function (key, value) {
            this[key] = value
        },
        Get: function (key) {
            return this[key]
        },
        Contains: function (key) {
            return this.Get(key) == null ? false : true
        },
        Remove: function (key) {
            delete this[key]
        }
    };
});

router.get('/keyword_stat', function (req, res) {
    var re = new RegExp('我是歌手');
    var ret = {};
    KeywordStat.find({$or: [
        {'title': re},
        {'trim_content.content': re}
    ]}).lean().exec(function (err, docs) {
        if (err) {
            console.log('error');
        }
        ret.status = true;
        ret.data = docs;

        res.json(ret);

    });
});


// 关键词 高频词统计
router.get('/hf_keywords', function (req, res) {
    //url: http://localhost:18222/hf_keywords/
    var ret = {}, list = [], keywordsList = [], tem = {}, arrW = [], arrN = [], bubble = {}, exportData = [];

    Wechat.find({}, {'keywords': 1}).lean().exec(function (err, docs) {
        if (err) {
            console.log('error');
        }

        //统计次数
        for (var i in  docs) {
            if (docs[i].keywords) {
                var words = docs[i].keywords;
                for (var w = 0, len = words.length; w < len; w++) {
                    if (!hashMap.Contains(words[w])) {
                        //not exist
                        hashMap.Set(words[w], words[w]);
                        tem[words[w]] = {
                            'keyword': words[w],
                            'num': 1
                        }
                    } else {
                        tem[words[w]].num++;
                    }
                }
            }
        }

        //排序
        for (var t in tem) {
            arrW.push(t);
            arrN.push(tem[t].num);
        }
        bubble = DoubleBubbleSort(arrN, arrW);

        //show
        var count = 50;//取前面count个
        for (var b = 0; b < count; b++) {
            exportData.push({
                'keywords': bubble.arr2[b],
                'count': bubble.arr1[b]
            })
//            exportData.push(bubble.arr2[b]);
        }

        ret = exportData;
        res.json(ret);
    });

    var hashMap = {
        Set: function (key, value) {
            this[key] = value
        },
        Get: function (key) {
            return this[key]
        },
        Contains: function (key) {
            return this.Get(key) == null ? false : true
        },
        Remove: function (key) {
            delete this[key]
        }
    };
});


module.exports = router;


